<template>
    <view class="cart-pay">
		<u-popup v-model="shoppingShow" mode="bottom" height="75%" border-radius="20">
				<ShoppingCart class="card-shop-wrap" :data="shoppingCart" :columns="columns" @numberComputed="numberComputed" />
		</u-popup>
		<u-modal v-model="modelShow" :content="content"></u-modal>
        <view class="h-110"></view>
        <view class="h-110 fcb px-32 safe-padding operation bg-white">
            <view class="flex items-center">
                <view class="cart" @click="pay">
                    <view class="badge">{{goodsNum}}</view>
                </view>
                <view class="ml-25 text-30">￥{{subtotal}}</view>
            </view>
            <view class="fcc">
                <view
                        class="pay fcc text-30 h-80 text-white "
                        @click="pay">去结算
                </view>
            </view>
        </view>
    </view>
</template>

<script>
	import ShoppingCart from '@/components/ShoppingCart.vue'
	import { isNull } from 'util';
export default {
	components:{
		'ShoppingCart':ShoppingCart
	},
	data(){
		return{
			shoppingShow: false,
			shoppingCart: [],
			cartShopData:[],
			modelShow:false,
			content:'请先在右上角选择您的桌位'
		}
	},
    props:{
		data:{
			type:Array,
			default:()=>[]
		},
		columns:{
		  type:Array,
		  default:()=>[]
		},
        goodsNum:{
            type:Number,
            default:0
        },
		subtotal:{
			type:Number,
			default:0
		},
        disabled:{
            type:Boolean,
            default:false
        },
        coupon:{
            type:Boolean,
            default: false
        },
        // 是否是堂食
        forHere:{
            type:Boolean,
            default: true
        }
    },
	onLoad() {
		
	},
    methods:{
        pay(){
			if (this.$store.getters.dineStyle=='外卖'){
				this.content="请先在右上角选择您的地址"
				let addrId = this.$store.getters.addrId
				if(addrId === '' || isNull(addrId)){
					this.modelShow = true
					return
				}
			} else if (this.$store.getters.dineStyle=='堂食'){
				content:'请先在右上角选择您的桌位'
				let table = this.$store.getters.table
				if(table === '' || isNull(table)){
					this.modelShow = true
					return
				}
			}
			
            // this.$emit('ok')
			this.shoppingCart = this.$store.getters.shoppingCart
			this.cartShopData = this.$store.getters.cartShopData
			// console.log(this.$store.getters.cartShopData)
			this.shoppingShow = true
			// console.log(this.data)
			// console.log(this.columns)
        },
		numberComputed(val,item,ite){
		    this.cartShopData.forEach(itemWrap=>{
		       let num = 0,sum = 0;
		        if(ite.id===itemWrap.id){
		            itemWrap.children.forEach(childItem=>{
						console.log(childItem)
						console.log(val)
		                if(childItem.id===item.id){
							if((isNull(childItem.goodNum) || childItem.goodNum == 0) && val == 1){
								childItem.goodNum = val
								this.$store.dispatch('addToCart',childItem)
							} else if (childItem.goodNum == 1 &&  val == 0){
								this.$store.dispatch('removeFromCart',childItem)
							} else {
								childItem.goodNum = val
								this.$store.dispatch('updateGoodNum',childItem,val)
							} 
		                    childItem.goodNum = val
							sum += childItem.price
							// this.subtotal += childItem.price
		                }
		                num += childItem.goodNum
		            })
					// console.log("----")
					// console.log(itemWrap.goodNum)
					// console.log(num)
					// console.log(sum)
					// console.log("----")
					// if(itemWrap.goodNum == undefined || itemWrap.goodNum < num){
					// 	this.subtotal += sum
					// }else{
					// 	this.subtotal -= sum
					// }
		            itemWrap.goodNum = num
		        }
		        let findObj = this.columns.find(it=>it.id===itemWrap.id)
		        if(findObj){
		            this.$set(findObj,'goodNum',itemWrap.goodNum)
		        }
		    })
		
		    this.goodsNum = this.columns.reduce((curr,next)=>{
		        return curr+(next.goodNum||0)
		    },0)
			console.log(this.columns)
			console.log(this.goodsNum)
			// console.log(this.subtotal)
			this.subtotal = this.$store.getters.subtotal
			this.$store.dispatch('updateCartShopData', this.cartShopData);
			console.log(this.$store.getters.cartShopData)
			console.log(this.$store.getters.subtotal)
			console.log(this.$store.getters.shoppingCart)
		},
    }
}
</script>

<style lang="scss" scoped>

.card-shop-wrap {
	flex: 1;
	display: flex;
	overflow: hidden;
}

.text-30{
	fonts-size:30rpx;
}
.ml-25{
	margin-left: 25rpx;
}
.h-80{
	height: 80rpx;
}
.h-110{
	height: 110rpx;
}
.bg-white{
	background: white;
}
.fcb{
	display: flex;
	align-items: center;
	justify-content: space-between;
}
.flex{
	display: flex;
}
.items-center{
	align-items: center;
}
.px-32{
	padding-left: 32rpx;
	padding-right: 32rpx;
}
.text-white{
	color: white;
}
.fcc{
	display: flex;
	align-items: center;
	justify-content: center;
}
.operation{
  border-top: 2px solid #F5F5F5;
  position: fixed;
  right: 0;
  bottom: 0;
  box-sizing: border-box;
  left: 0;
  z-index: 119;
}
.cart{
  width: 104rpx;
  height: 104rpx;
  background: url("~@/static/cart.png");
  background-size: cover;
    position: relative;
}
.eat{
  width: 200rpx;
  height: 82rpx;
  background: #000000;
  border-radius: 200rpx 200rpx 200rpx 200rpx;

}
.pay{
  background:  #9E2A22;
  width: 168rpx;
  height: 82rpx;
  border-radius: 200rpx 200rpx 200rpx 200rpx;
}
.disabled{
  background: #fd545b80;
}
.coupon{
  width: 126rpx;
  height: 36rpx;
  background: #E2E2E2;
  border-radius: 200rpx 200rpx 200rpx 200rpx;
  color: #666666;

}
.use-coupon{
  height: 36rpx;
  background: rgba(158, 42, 34, 0.3);
  border-radius: 200rpx 200rpx 200rpx 200rpx;
  padding-left: 8rpx;
  padding-right: 8rpx;
  color: #9E2A22;
  display: inline-block;
  line-height: 36rpx;
  font-size: 20rpx;

}
.badge{
    right: -2rpx;
    top: -2rpx;
    background-color: #9E2A22;
    position: absolute;
    display: flex;
    overflow: hidden;
    box-sizing: border-box;
    justify-content: center;
    flex-direction: row;
    height: 20px;
    min-width: 20px;
    padding: 0 4px;
    line-height: 18px;
    color: #fff;
    border-radius: 100px;
    border: 1px solid #fff;
    text-align: center;
    font-family: Helvetica Neue,Helvetica,sans-serif;
    -webkit-font-feature-settings: "tnum";
    font-feature-settings: "tnum";
    font-size: 12px;
    z-index: 9;
    cursor: pointer;
}
</style>
